package com.qire.other.thirdPlatform;

import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;

import com.alipay.sdk.app.PayTask;
import com.qire.other.thirdPlatform.pay.ThirdPlatformPay;
import com.qire.other.thirdPlatform.pay.ThirdPlatformPayCallBack;

import java.util.Map;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 阿里平台相关操作，如支付等
 */
public final class AliPayPlatform extends ThirdPlatform implements ThirdPlatformPay<String> {

    public static final int MSG_PAY_BACK = 102;

    public static final AliPayPlatform AliPay = new AliPayPlatform();

    private SimpleThreadFactory threadFactory = new SimpleThreadFactory("AliPayPlatform");

    private ThirdPlatformPayCallBack<String> mCallBack;

    private AliPayPlatform() {}

    private Bundle buildResult(Map<String, String> rawResult) {
        Bundle data = new Bundle();

        if(rawResult != null) {
            String resultStatus, result, memo;

            if((resultStatus = rawResult.get("resultStatus")) != null) {
                data.putString("resultStatus", resultStatus);
            }

            if((result = rawResult.get("result")) != null) {
                data.putString("result", result);
            }

            if((memo = rawResult.get("memo")) != null) {
                data.putString("memo", memo);
            }
        }

        return data;
    }

    @Override
    public void goPay(final String orderInfo, final Activity activity, ThirdPlatformPayCallBack callBack) {
        this.mCallBack = callBack;
        threadFactory.newThread(() -> {
            PayTask payTask = new PayTask(activity);
            Map<String, String> result = payTask.payV2(orderInfo, true);
            Message msg = Message.obtain();
            msg.what = MSG_PAY_BACK;
            msg.setData(buildResult(result));
            sendMessage(msg);
        }).start();
    }

    @Override
    public void handleMessage(Message msg) {
        int tag = msg.what;
        switch (tag) {
            case MSG_PAY_BACK:
                noticePaymentResult(msg.getData());
                return;
        }
    }

    private void noticePaymentResult(Bundle bundle) {
        String resultInfo = bundle.getString("result");// 同步返回需要验证的信息
        String resultStatus = bundle.getString("resultStatus");
        // 判断resultStatus 为9000则代表支付成功
        if (TextUtils.equals(resultStatus, "9000")) {
            // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
            mCallBack.callBack("支付成功", PAY_SUCCESS, resultInfo);
        } else {
            // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
            mCallBack.callBack("支付失败", PAY_ERROR, resultInfo);
        }
    }

    public static class SimpleThreadFactory implements ThreadFactory {
        private final String namePrefix;
        private final AtomicInteger nextId = new AtomicInteger(1);
        // 定义线程组名称，在 jstack命令 问题排查时，非常有帮助
        SimpleThreadFactory(String whatGroupName) {
            namePrefix = "From SimpleThreadFactory's " + whatGroupName + "-Worker-";
        }

        @Override
        public Thread newThread(Runnable task) {
            String name = namePrefix + nextId.getAndIncrement();
            Thread thread = new Thread(null, task, name, 0);
            System.out.println(thread.getName());
            return thread;
        }
    }

}
